package com.mk.admin.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
<<<<<<< HEAD
import com.mk.admin.mapper.SelfBillingMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.*;
=======
import com.mk.admin.mapper.DictionaryMapper;
import com.mk.admin.mapper.SelfBillingMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.*;
import com.mk.admin.model.entity.DictionaryEntity;
import com.mk.admin.model.entity.SelfBillingApiEntity;
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
import com.mk.admin.model.entity.SelfBillingEntity;
import com.mk.admin.model.query.SelfBillingProjectRemoteQuery;
import com.mk.admin.model.vo.*;
import com.mk.admin.service.PayService;
import com.mk.admin.service.PrescriptionService;
import com.mk.admin.service.SelfBillingService;
import com.mk.admin.service.WeChatPayService;
import com.mk.admin.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
<<<<<<< HEAD
=======
import java.util.Map;
import java.util.TreeMap;
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac

@Service
public class SelfBillingServiceImpl implements SelfBillingService {
    @Autowired
    private SelfBillingMapper selfBillingMapper;
    @Autowired
    private PrescriptionService prescriptionService;
    @Autowired
    private PayService payService;
    @Autowired
    private WeChatPayService weChatPayService;
<<<<<<< HEAD
=======
    @Autowired
    private DictionaryMapper dictionaryMapper;
    /**
     * 将api对应的值存入map
     */
    private final Map<String, String> mapApi = new TreeMap<>();
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac

    @Override
    public ResultDto select(SelfBillingVo vo) {
//        取查询已有的开单记录，根据远程已有的状态改变表中的状态
//
        SelfBillingEntity entity = JSONCopy.copy(vo, SelfBillingEntity.class);
        List<SelfBillingDto> select = selfBillingMapper.select(entity);

        return ResultDto.data(selfBillingMapper.count(entity), select);
    }

    @Override
    public ResultDto cancel(SelfBillingVo vo) {
        HttpTool.postJson("opt_0096", vo);
        SelfBillingEntity entity = new SelfBillingEntity();
        entity.setPatId(vo.getPatId());
        entity.setPreId(vo.getPreId());
        List<SelfBillingDto> selfBillingDtoList = selfBillingMapper.select(entity);
        if (selfBillingDtoList.size() > 0) {
            entity = JSONCopy.copy(selfBillingDtoList.get(0), SelfBillingEntity.class);
            entity.setPayStatus(vo.getPayStatus());
            entity.setPayStatusName(vo.getPayStatusName());
            selfBillingMapper.updateById(entity);
        }
        return ResultDto.data("");
    }

    @Override
    public ResultDto refund(SelfBillingVo vo) {
<<<<<<< HEAD
=======
// 门诊收费退费
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
        JSONObject jsonObject0006 = getPatientRegisterReturnMoneyJson(vo);
        Object result0006 = jsonObject0006.get("result");
        if (result0006 == null || "[]".equals(result0006)) {
            return ResultDto.data("error", jsonObject0006);
        }
        //        微信退费pay_0327
        JSONObject jsonObject0327 = wechatPayRefund(vo);
        Object result0327 = jsonObject0327.get("result");
        if (result0327 == null || "[]".equals(result0327)) {
            return ResultDto.data("error", jsonObject0327);
        }
//        微信退费查询pay_0328
        JSONObject jsonObject0328 = wechatPayRefundSelect(vo);
        Object result0328 = jsonObject0328.get("result");
        if (result0328 == null || "[]".equals(result0328)) {
            return ResultDto.data("error", jsonObject0328);
        }
        SelfBillingEntity entity = new SelfBillingEntity();
        entity.setId(vo.getId());
        entity.setPayStatus(vo.getPayStatus());
        entity.setPayStatusName(vo.getPayStatusName());
<<<<<<< HEAD
        return ResultDto.data("success", "退款成功");
=======
        return ResultDto.data("success", selfBillingMapper.updateById(entity));
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
    }

    private JSONObject wechatPayRefundSelect(SelfBillingVo vo) {
        WeChatRefundSelectVo weChatRefundSelectVo = new WeChatRefundSelectVo();
        weChatRefundSelectVo.setOrderId(vo.getOrderId());
        return weChatPayService.refundSelectJson(weChatRefundSelectVo);
    }

    private JSONObject wechatPayRefund(SelfBillingVo vo) {
        WeChatRefundVo weChatRefundVo = new WeChatRefundVo();
        weChatRefundVo.setOrderId(vo.getOrderId());
        weChatRefundVo.setRequestSn(vo.getOrderId());
        weChatRefundVo.setRefundMoney(vo.getMoney());
        weChatRefundVo.setPayMoney(vo.getMoney());
        weChatRefundVo.setRefundDesc(vo.getPayStatusName());
//            微信退款
        return weChatPayService.refundJson(weChatRefundVo);
    }

    private JSONObject getPatientRegisterReturnMoneyJson(SelfBillingVo vo) {
        PatientRegisterReturnMoneyVo patientRegisterReturnMoneyVo = new PatientRegisterReturnMoneyVo();
        patientRegisterReturnMoneyVo.setPatId(vo.getPatId());
        patientRegisterReturnMoneyVo.setReceiptNo(vo.getReceiptNo());
        patientRegisterReturnMoneyVo.setTotalAmount(vo.getMoney());
        patientRegisterReturnMoneyVo.setRefundMoney(vo.getMoney());
        patientRegisterReturnMoneyVo.setPayType(vo.getPayType() + "");
        patientRegisterReturnMoneyVo.setPayMoney(vo.getMoney());
        return payService.patientRegisterReturnMoneyJson(patientRegisterReturnMoneyVo);
    }


    @Override
    public ResultDto add(SelfBillingVo vo) {
        SelfBillingEntity entity = JSONCopy.copy(vo, SelfBillingEntity.class);
        entity.setMoney(NumberTool.strToMoney(vo.getMoney()));
        return ResultDto.data(selfBillingMapper.insert(entity));
    }

    @Override
    public ResultDto update(SelfBillingVo vo) {
        SelfBillingEntity entity = JSONCopy.copy(vo, SelfBillingEntity.class);
        entity.setMoney(NumberTool.strToMoney(vo.getMoney()));
        return ResultDto.data(selfBillingMapper.updateById(entity));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(selfBillingMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(selfBillingMapper.status(id));
    }

    @Override
    public ResultDto addBatch(SelfBillingBatchVo vo) {
        List<SelfBillingVo> list = vo.getList();
        String res = "";
<<<<<<< HEAD
=======
        if (mapApi.size() == 0) {
            setApiMap();
        }
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
        for (SelfBillingVo item : list
        ) {
            JSONObject in = new JSONObject();
            in.put("patId", item.getPatId());
            in.put("recipeNumber", "1");
            in.put("feeItemCode", item.getCode());
            in.put("billDoctorCode", item.getDoctorCode());
            in.put("execDeptCode", item.getDepartmentCode());
<<<<<<< HEAD
            JSONObject jsonObject = HttpTool.postJson("opt_0095", in);
=======
            JSONObject jsonObject = HttpTool.postJson(mapApi.get(item.getMedicalType()), in);
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
            Object result = jsonObject.get("result");
            if (result != null) {
                List<SelfBillingRemoteDto> remoteDtoList = JSONArray.parseArray(result.toString(), SelfBillingRemoteDto.class);
                if (remoteDtoList.size() > 0) {
                    SelfBillingEntity entity = new SelfBillingEntity();
                    entity.setPreId(remoteDtoList.get(0).getPreId());
                    List<SelfBillingDto> dtoList = selfBillingMapper.select(entity);
                    if (dtoList.size() == 0) {
                        entity = JSONCopy.copy(item, SelfBillingEntity.class);
                        entity.setPreId(remoteDtoList.get(0).getPreId());
                        entity.setCreateTime(DateTool.format());
                        entity.setMoney(NumberTool.strToMoney(item.getMoney()));
                        entity.setPayStatus(0);
<<<<<<< HEAD
=======
                        entity.setCanReserve(item.getCanReserve());
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
                        entity.setPayStatusName("未支付");
                        selfBillingMapper.insert(entity);
                    } else {
//                        如果有值，则表示已经预约了，无需再次预约
                        res += item.getName() + ",";
                    }
                }
            }
        }
        return ResultDto.data("success", res);
    }
<<<<<<< HEAD
=======


    /**
     * 得到数据库配置的api对应的值
     */
    private void setApiMap() {
        DictionaryEntity entity = new DictionaryEntity();
        entity.setStatus(1);
        entity.setCode("SetArraySelfBillingApi");

        List<DictionaryDto> list = dictionaryMapper.select(entity);
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            result.append(list.get(0).getDescribe());
        }
        List<SelfBillingApiEntity> selfBillingApiEntityList = JSONArray.parseArray(result.toString(), SelfBillingApiEntity.class);
        for (SelfBillingApiEntity apiEntity : selfBillingApiEntityList
        ) {
            mapApi.put(apiEntity.getType(), apiEntity.getApi());
        }
    }

>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
}
